package com.bishe.bishe.Service.ServiceImpl;

import com.bishe.bishe.Service.CustomerService;
import com.bishe.bishe.dao.mapper.CustomerMapper;
import com.bishe.bishe.entity.PageResult;
import com.bishe.bishe.pojo.Customer;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService
{
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult <Customer> findall(int page, int size) {
        PageHelper.startPage(page, size);
        Page <Customer> pageResult = (Page <Customer>) customerMapper.selectAll();
        return new PageResult <>(pageResult.getTotal(), pageResult.getResult());
    }

    @Override
    public List <Customer> findList(Map <String, Object> searchMap) {
        Example example = new Example(Customer.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("customerName", "%" + (String) searchMap.get("name") + "%");
            }
        }
        return customerMapper.selectByExample(example);
    }

    @Override
    public PageResult <Customer> findPage(Map <String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = new Example(Customer.class);
        Example.Criteria criteria = example.createCriteria();

        if (searchMap != null) {
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("customerName", "%" + (String) searchMap.get("name") + "%");
            }

        }
        Page <Customer> pageResult = (Page <Customer>) customerMapper.selectByExample(example);
        return new PageResult <>(pageResult.getTotal(), pageResult.getResult());
    }

    @Override
    public int delete(int customerId) {
        return customerMapper.deleteByPrimaryKey(customerId);
    }

    @Override
    public int update(Customer customer) {
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    @Override
    public Integer insert(Customer customer) {
        java.util.Date date=new java.util.Date();
        long l = date.getTime();
        java.sql.Date sqlDate=new java.sql.Date(l);
        customer.setCustomerDate(sqlDate);
        customerMapper.insert(customer);
        return customer.getCustomerId();

    }

    @Override
    public int findByNumber(Customer customer) {
        Customer byNumber = customerMapper.findByNumber(customer.getCustomerNumber());
        return byNumber.getCustomerId();
    }

    @Override
    public Customer select(Customer customer) {
        return customerMapper.findByNumber(customer.getCustomerNumber());
    }

    @Override
    public int findcustomer(Customer customer) {
        Customer customer1 = customerMapper.findCustomer(customer);
        return customer1.getCustomerId();
    }

    @Override
    public List <Customer> count() {
        return customerMapper.count();
    }

    @Override
    public List <Customer> findall1() {
        return customerMapper.findall1();
    }

    @Override
    public Customer findCustomer1(String customerNumber) {
        return customerMapper.findCustomer1(customerNumber);
    }
}
